/**
 * 
 */
package cn.pfcg.rally.persistent.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import cn.pfcg.rally.domain.bean.Question;
import cn.pfcg.rally.domain.bean.SelectQuestion;
import cn.pfcg.rally.persistent.dao.QuestionDAO;

/**
 * @author cxq
 *
 */
public class QuestionDAOImpl implements QuestionDAO {
	private static Log log=LogFactory.getLog(QuestionDAOImpl.class);
	private HibernateTemplate hibernateTemplate;

	/* (non-Javadoc)
	 * @see cn.pfcg.rally.persistent.dao.QuestionDAO#getQuestionByNumber(java.lang.String)
	 */
	public Question getQuestionByNumber(String questionNumber) {
		return (SelectQuestion) hibernateTemplate.find("from Question question " + "where question.number = ?" , questionNumber).get(0);
	}

	/* (non-Javadoc)
	 * @see cn.pfcg.rally.persistent.dao.QuestionDAO#saveQuestion(cn.pfcg.rally.persistent.po.SelectQuestion)
	 */
	public void saveQuestion(Question questionPO) {
				hibernateTemplate.save(questionPO);
	}
	
	/**
	 * @param hibernateTemplate the hibernateTemplate to set
	 */
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public void deleteQuestion(String questionNumber) {
		Question question = (Question) hibernateTemplate.find("from Question question " + "where question.number = ?" , questionNumber).get(0);
		hibernateTemplate.delete(question);
	}

	public List getAllQuestion() {
		return hibernateTemplate.find("from Question");
	}

	public void updateQuestion(String questionNumber,final Question question) {
				hibernateTemplate.update(question);
	}

	public String getLastQuestionNumber() {
		Question question = null;
		List list = (ArrayList) hibernateTemplate.find("from Question question order by question.id desc");
		if(list.size()!=0) {
			question = (Question)list.get(0);
			return question.getNumber();
		}else{
			return null;
		}
		
			
	}
}
